package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.bean.MembersBean;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.StudentListByGroupPresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IStudentListByGroupView;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.SheetDialog;
import com.konne.nightmare.FastPublicOpinion.ui.information.adapter.MembersManagerMAdapter;
import com.konne.nightmare.FastPublicOpinion.utils.GlideUtils;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.shehuan.niv.NiceImageView;

import java.util.List;

import butterknife.BindView;

/**
 * Create by ws.
 * on Data:2022/1/26  Time:上午 11:10.
 * description: 小组下的学生列表 .
 */
public class StudentListByGroupFragment extends BaseMvpFragment<IStudentListByGroupView, StudentListByGroupPresenter> implements IStudentListByGroupView {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    public static StudentListByGroupFragment getInstance(){
        return new StudentListByGroupFragment();
    }

    @Override
    protected StudentListByGroupPresenter createPresenter() {
        return new StudentListByGroupPresenter();
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    public int getLayout() {
        return R.layout.frag_student_list_by_group;
    }

    @Override
    public void initView() {
        SuperTextView stv_sub_title=findViewById(R.id.stv_sub_title);
        stv_sub_title.setShowState(false);
        stv_sub_title.setPadding(0,0,0,0);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        findViewById(R.id.stv_manager).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showManager();
            }
        });
    }
    private void showManager(){
        new SheetDialog(getContext())
                .addSheet(new SheetDialog.Sheet("设定组长",0,0))
                .addSheet(new SheetDialog.Sheet("添加组员",0,1))
                .addSheet(new SheetDialog.Sheet("移出组员",0,2))
                .addSheet(new SheetDialog.Sheet("修改组名",0,3))
                .addSheet(new SheetDialog.Sheet("删除该组",0xffF9485D,4))
                .setSheetListener(new SheetDialog.SheetListener() {
                    @Override
                    public void sheet(SheetDialog.Sheet sheet) {
                        switch (sheet.action){
                            case 0:
                                mPresenter.setTeamLeader();
                                break;
                            case 1:
                                mPresenter.addTeamUser();
                                break;
                            case 2:
                                mPresenter.removerUser();
                                break;
                            case 3:
                                mPresenter.renameGroupName();
                                break;
                            case 4:
                                mPresenter.delGroup();
                                break;
                        }
                    }
                }).show();
    }

    @Override
    public void finishAct() {
        getActivity().finish();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPresenter.init();
    }

    @Override
    public void updataGroupStudents(List<MembersBean> datas) {
        SLBGAdapter mAdapter= (SLBGAdapter) recyclerView.getAdapter();
        if(mAdapter==null){
            mAdapter=new SLBGAdapter(datas);
            recyclerView.setAdapter(mAdapter);
        }else {
            mAdapter.setNewData(datas);
        }

    }
    class SLBGAdapter extends BaseQuickAdapter<MembersBean, BaseViewHolder>{
        public SLBGAdapter(@Nullable List<MembersBean> data) {
            super(R.layout.item_menbers_user,data);
        }

        @Override
        protected void convert(BaseViewHolder helper, MembersBean item) {
            helper.setText(R.id.tv_name, item.getName())
                    .setVisible(R.id.stv_tag, false)
                    .setVisible(R.id.stv_chat, false);
            NiceImageView avatar = helper.getView(R.id.ic_avatar);
            GlideUtils.loadCircleImage(item.getAvatar(),avatar);
        }
    }
}
